import { _decorator, Component, Node, Sprite, Label, ImageAsset, EventTouch } from 'cc';
import {oops} from "db://oops-framework/core/Oops";
import {imageAsset2Sprite} from "db://assets/script/utils/imageUtils";
const { ccclass, property } = _decorator;

@ccclass('GameItemView')
export class GameItemView extends Component {
    @property({
        type: Sprite,
        tooltip: "游戏 icon"
    })
    icon: Sprite = null!

    @property({
        type: Label,
        tooltip: "游戏名"
    })
    gameName: Label = null!

    @property({
        type: Node,
        tooltip: "选中游戏"
    })
    checkMark: Node = null!

    @property({
        type: Node,
        tooltip: "分割线"
    })
    line: Node = null!
    clickCallback: any
    // 第几个入口
    index: number
    start() {
        this.node.on(Node.EventType.TOUCH_START, this.onClick.bind(this))
    }

    onClick(event: EventTouch) {
        this.selected()
        if (this.clickCallback) {
            this.clickCallback(this.index)
        }
    }

    // updateGameIcon(gameType: string) {
    //
    // }

    setOnClickCallback(cb, index) {
        this.clickCallback = cb
        this.index = index
    }

    // 设置成麻将
    setMajiang() {
        oops.res.load("gui/texture/icon-majiang", ImageAsset, (err: Error, icon: ImageAsset) => {
            if (err) {
                console.error("laod icon fail")
            } else {
                this.icon.spriteFrame = imageAsset2Sprite(icon)
            }
        })
    }

    updateGameName(gameName: string) {
        this.gameName.string = gameName
    }

    // 不显示分割线
    noLine() {
        this.line.active = false
    }

    noSelected() {
        this.checkMark.active = false
    }

    selected() {
        this.checkMark.active = true
    }
}


